package com.xiaoshuidi.cloud.module.rooms.job.xxljob;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.module.rooms.framework.config.ConfigValues;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomHFMetro;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomHFMetroRepository;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class SyncHFMetroJob {

    private final ConfigValues configValues;
    private final RoomHFMetroRepository roomHFMetroRepository;

    /**
     * 同步合肥地铁信息到数据库
     */
    @TenantIgnore
    @XxlJob("sync-hf-metro")
    @Transactional(rollbackFor = Exception.class)
    public void syncHFMetro() {
        log.info("同步合肥地铁信息到数据库 -- 开始");

        // 清除原数据
        LambdaQueryWrapper<RoomHFMetro> wrapper = Wrappers.lambdaQuery();
        roomHFMetroRepository.remove(wrapper);
        // 插入新数据
        String hfMetro = configValues.getHFMetro();
        String resp = HttpUtil.get(hfMetro);
        JSONObject jsonObject = JSONObject.parseObject(resp);
        JSONArray lineInfos = (JSONArray) jsonObject.get("l");
        for (int i = 0; i < lineInfos.size(); i++) {
            JSONObject lineInfoObj = (JSONObject) lineInfos.get(i);
            String lineName = lineInfoObj.get("ln").toString();
            RoomHFMetro roomHFMetro = new RoomHFMetro()
                    .setName(lineName)
                    .setSort(i + 1)
                    .setLevel(1);
            roomHFMetroRepository.save(roomHFMetro);

            JSONArray siteInfos = (JSONArray) lineInfoObj.get("st");
            List<RoomHFMetro> roomHFMetros = new ArrayList<>();
            for (int j = 0; j < siteInfos.size(); j++) {
                JSONObject siteInfoObj = (JSONObject) siteInfos.get(j);
                String siteName = siteInfoObj.get("n").toString();
                String latAndLon = siteInfoObj.get("sl").toString();
                String[] split = latAndLon.split(",");
                RoomHFMetro metroSite = new RoomHFMetro()
                        .setName(siteName)
                        .setPid(roomHFMetro.getId())
                        .setLevel(2)
                        .setSort(j + 1)
                        .setLat(split[1])
                        .setLon(split[0]);
                roomHFMetros.add(metroSite);
            }
            roomHFMetroRepository.saveBatch(roomHFMetros);
        }
        log.info("同步合肥地铁信息到数据库 -- 完成");
    }
}
